package com.cn.ServiceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cn.Domain.CheckGroup;
import com.cn.Entity.PageResult;
import com.cn.Mapper.CheckGroupAndItemMapper;
import com.cn.Mapper.CheckGroupMapper;
import com.cn.Service.CheckGroupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupAndItemMapper checkGroupAndItemMapper;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //添加至t_checkgroup
        checkGroupAndItemMapper.add(checkGroup);
        Integer checkGroupId = checkGroupAndItemMapper.selectPrimaryKey();//检查组id
        addCheckGroupAndCheckItem(checkGroupId,checkitemIds);

    }

    @Override
    public PageResult findbyPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize,queryString);
        Page<CheckGroup> checkGroups = checkGroupMapper.selectByCondition(queryString);
        return new PageResult(checkGroups.getTotal(),checkGroups.getResult());
    }

    @Override
    public void delete(Integer id) {
        checkGroupAndItemMapper.deleteByCheckGroupId(id);
        checkGroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupAndItemMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        //先清理关系再添加
        checkGroupAndItemMapper.deleteByCheckGroupId(checkGroupId);

        Map<String,Integer> map = new HashMap<>();
        for (Integer checkitemId : checkitemIds) {
            map.put("checkgroup_id",checkGroupId);
            map.put("checkitem_id",checkitemId);
            checkGroupAndItemMapper.setCheckGroupAndCheckItem(map);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.selectAll();
    }

    public void addCheckGroupAndCheckItem(Integer CheckGroupId,Integer[] checkitemIds){
 if (checkitemIds!=null&&checkitemIds.length>0){
     for (Integer checkitemId : checkitemIds) {
         Map<String,Integer> map = new HashMap<>();
         map.put("checkgroup_id",CheckGroupId);
         map.put("checkitem_id",checkitemId);
         checkGroupAndItemMapper.setCheckGroupAndCheckItem(map);
     }
 }
    }
}
